home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / demo / wemdemo4.zip / INFO / ELISP.6 < prev    next >
Text File  |  1994-09-21  |  51KB  |  1,243 lines

  1. Info file elisp, produced by Makeinfo, -*- Text -*- from input file
  2. elisp.texi.
  3.  
  4.    This file documents GNU Emacs Lisp.
  5.  
  6.    This is edition 1.03 of the GNU Emacs Lisp Reference Manual,   for
  7. Emacs Version 18.
  8.  
  9.    Published by the Free Software Foundation, 675 Massachusetts
  10. Avenue,  Cambridge, MA 02139 USA
  11.  
  12.    Copyright (C) 1990 Free Software Foundation, Inc.
  13.  
  14.    Permission is granted to make and distribute verbatim copies of
  15. this manual provided the copyright notice and this permission notice
  16. are preserved on all copies.
  17.  
  18.    Permission is granted to copy and distribute modified versions of
  19. this manual under the conditions for verbatim copying, provided that
  20. the entire resulting derived work is distributed under the terms of a
  21. permission notice identical to this one.
  22.  
  23.    Permission is granted to copy and distribute translations of this
  24. manual into another language, under the above conditions for modified
  25. versions, except that this permission notice may be stated in a
  26. translation approved by the Foundation.
  27.  
  28.  
  29. 
  30. File: elisp,  Node: Combining Conditions,  Next: Iteration,  Prev: Conditionals,  Up: Control Structures
  31.  
  32. Constructs for Combining Conditions
  33. ===================================
  34.  
  35.    This section describes three constructs that are often used
  36. together with `if' and `cond' to express complicated conditions.  The
  37. constructs `and' and `or' can also be used individually as kinds of
  38. multiple conditional constructs.
  39.  
  40.  * Function: not CONDITION
  41.      This function tests for the falsehood of CONDITION.  It returns
  42.      `t' if CONDITION is `nil', and `nil' otherwise.  The function
  43.      `not' is identical to `null', and we recommend using `null' if
  44.      you are testing for an empty list.
  45.  
  46.  * Special Form: and CONDITIONS...
  47.      The `and' special form tests whether all the CONDITIONS are
  48.      true.  It works by evaluating the CONDITIONS one by one in the
  49.      order written.
  50.  
  51.      If any of the CONDITIONS evaluates to `nil', then the result of
  52.      the `and' must be `nil' regardless of the remaining CONDITIONS;
  53.      so the remaining CONDITIONS are ignored and the `and' returns
  54.      right away.
  55.  
  56.      If all the CONDITIONS turn out non-`nil', then the value of the
  57.      last of them becomes the value of the `and' form.
  58.  
  59.      Here is an example.  The first condition returns the integer 1,
  60.      which is not `nil'.  Similarly, the second condition returns the
  61.      integer 2, which is not `nil'.  The third condition is `nil', so
  62.      the remaining condition is never evaluated.
  63.  
  64.           (and (print 1) (print 2) nil (print 3))
  65.                -| 1
  66.                -| 2
  67.           => nil
  68.  
  69.      Here is a more realistic example of using `and':
  70.  
  71.           (if (and (consp foo) (eq (car foo) 'x))
  72.               (message "foo is a list starting with x"))
  73.  
  74.      Note that `(car foo)' is not executed if `(consp foo)' returns
  75.      `nil', thus avoiding an error.
  76.  
  77.      `and' can be expressed in terms of either `if' or `cond'.  For
  78.      example:
  79.  
  80.           (and ARG1 ARG2 ARG3)
  81.           ==
  82.           (if ARG1 (if ARG2 ARG3))
  83.           ==
  84.           (cond (ARG1 (cond (ARG2 ARG3))))
  85.  
  86.  * Special Form: or CONDITIONS...
  87.      The `or' special form tests whether at least one of the
  88.      CONDITIONS is true.  It works by evaluating all the CONDITIONS
  89.      one by one in the order written.
  90.  
  91.      If any of the CONDITIONS evaluates to a non-`nil' value, then
  92.      the result of the `or' must be non-`nil'; so the remaining
  93.      CONDITIONS are ignored and the `or' returns right away.  The
  94.      value it returns is the non-`nil' value of the condition just
  95.      evaluated.
  96.  
  97.      If all the CONDITIONS turn out `nil', then the `or' expression
  98.      returns `nil'.
  99.  
  100.      For example, this expression tests whether `x' is either 0 or
  101.      `nil':
  102.  
  103.           (or (eq x nil) (= x 0))
  104.  
  105.      Like the `and' construct, `or' can be written in terms of
  106.      `cond'.  For example:
  107.  
  108.           (or ARG1 ARG2 ARG3)
  109.           ==
  110.           (cond (ARG1)
  111.                 (ARG2)
  112.                 (ARG3))
  113.  
  114.      You could almost write `or' in terms of `if', but not quite:
  115.  
  116.           (if ARG1 ARG1
  117.             (if ARG2 ARG2 
  118.               ARG3))
  119.  
  120.      This is not completely equivalent because it can evaluate ARG1
  121.      or ARG2 twice.  By contrast, `(or ARG1 ARG2 ARG3)' never
  122.      evaluates any argument more than once.
  123.  
  124.  
  125. 
  126. File: elisp,  Node: Iteration,  Next: Nonlocal Exits,  Prev: Combining Conditions,  Up: Control Structures
  127.  
  128. Iteration
  129. =========
  130.  
  131.    Iteration means executing part of a program repetitively.  For
  132. example, you might want to repeat some expressions once for each
  133. element of a list, or once for each integer from 0 to N.  You can do
  134. this in Emacs Lisp with the special form `while':
  135.  
  136.  * Special Form: while CONDITION FORMS...
  137.      `while' first evaluates CONDITION.  If the result is non-`nil',
  138.      it evaluates FORMS in textual order.  Then it reevaluates
  139.      CONDITION, and if the result is non-`nil', it evaluates FORMS
  140.      again.  This process repeats until CONDITION evaluates to `nil'.
  141.  
  142.      There is no limit on the number of iterations that may occur. 
  143.      The loop will continue until either CONDITION evaluates to `nil'
  144.      or until an error or `throw' jumps out of it (*note Nonlocal
  145.      Exits::.).
  146.  
  147.      The value of a `while' form is always `nil'.
  148.  
  149.           (setq num 0)
  150.                => 0
  151.           (while (< num 4)
  152.             (princ (format "Iteration %d." num))
  153.             (setq num (1+ num)))
  154.                -| Iteration 0.
  155.                -| Iteration 1.
  156.                -| Iteration 2.
  157.                -| Iteration 3.
  158.                => nil
  159.  
  160.      If you would like to execute something on each iteration before
  161.      the end-test, put it together with the end-test in a `progn' as
  162.      the first argument of `while', as shown here:
  163.  
  164.           (while (progn
  165.                    (forward-line 1)
  166.                    (not (looking-at "^$"))))
  167.  
  168.      This moves forward one line and continues moving by lines until
  169.      an empty line is reached.
  170.  
  171.  
  172. 
  173. File: elisp,  Node: Nonlocal Exits,  Prev: Iteration,  Up: Control Structures
  174.  
  175. Nonlocal Exits
  176. ==============
  177.  
  178.    A "nonlocal exit" is a transfer of control from one point in a
  179. program to another remote point.  Nonlocal exits can occur in Emacs
  180. Lisp as a result of errors; you can also use them under explicit
  181. control.
  182.  
  183. * Menu:
  184.  
  185. * Catch and Throw::     Nonlocal exits for the program's own purposes.
  186. * Examples of Catch::   Showing how such nonlocal exits can be written.
  187. * Errors::              How errors are signaled and handled.
  188. * Cleanups::            Arranging to run a cleanup form if an error happens.
  189.  
  190.  
  191. 
  192. File: elisp,  Node: Catch and Throw,  Next: Examples of Catch,  Prev: Nonlocal Exits,  Up: Nonlocal Exits
  193.  
  194. Explicit Nonlocal Exits: `catch' and `throw'
  195. --------------------------------------------
  196.  
  197.    Most control constructs affect only the flow of control within the
  198. construct itself.  The function `throw' is the sole exception: it
  199. performs a nonlocal exit on request.  `throw' is used inside a
  200. `catch', and jumps back to that `catch'.  For example:
  201.  
  202.      (catch 'foo
  203.        (progn
  204.          ...
  205.            (throw 'foo t)
  206.          ...))
  207.  
  208. The `throw' transfers control straight back to the corresponding
  209. `catch', which returns immediately.  The code following the `throw'
  210. is not executed.  The second argument of `throw' is used as the
  211. return value of the `catch'.
  212.  
  213.    The `throw' and the `catch' are matched through the first
  214. argument: `throw' searches for a `catch' whose first argument is `eq'
  215. to the one specified.  Thus, in the above example, the `throw'
  216. specifies `foo', and the `catch' specifies the same symbol, so that
  217. `catch' is applicable.  If there is more than one applicable `catch',
  218. the innermost one takes precedence.
  219.  
  220.    All Lisp constructs between the `catch' and the `throw', including
  221. function calls, are exited automatically along with the `catch'. 
  222. When binding constructs such as `let' or function calls are exited in
  223. this way, the bindings are unbound, just as they are when the binding
  224. construct is exited normally (*note Local Variables::.).  Likewise,
  225. the buffer and position saved by `save-excursion' (*note
  226. Excursions::.) are restored, and so is the narrowing status saved by
  227. `save-restriction' and the window selection saved by
  228. `save-window-excursion' (*note Window Configurations::.).  Any
  229. cleanups established with the `unwind-protect' special form are
  230. executed if the `unwind-protect' is exited with a `throw'.
  231.  
  232.    The `throw' need not appear lexically within the `catch' that it
  233. jumps to.  It can equally well be called from another function called
  234. within the `catch'.  As long as the `throw' takes place
  235. chronologically after entry to the `catch', and chronologically
  236. before exit from it, it has access to that `catch'.  This is why
  237. `throw' can be used in commands such as `exit-recursive-edit' which
  238. throw back to the editor command loop (*note Recursive Editing::.).
  239.  
  240.      Common Lisp note: most other versions of Lisp, including Common
  241.      Lisp, have several ways of transferring control nonsequentially:
  242.      `return', `return-from', and `go', for example.  Emacs Lisp has
  243.      only `throw'.
  244.  
  245.  * Special Form: catch TAG BODY...
  246.      `catch' establishes a return point for the `throw' function. 
  247.      The return point is distinguished from other such return points
  248.      by TAG, which may be any Lisp object.  The argument TAG is
  249.      evaluated normally before the return point is established.
  250.  
  251.      With the return point in effect, the forms of the BODY are
  252.      evaluated in textual order.  If the forms execute normally,
  253.      without error or nonlocal exit, the value of the last body form
  254.      is returned from the `catch'.
  255.  
  256.      If a `throw' is done within BODY specifying the same value TAG,
  257.      the `catch' exits immediately; the value it returns is whatever
  258.      was specified as the second argument of `throw'.
  259.  
  260.  * Function: throw TAG VALUE
  261.      The purpose of `throw' is to return from a return point
  262.      previously established with `catch'.  The argument TAG is used
  263.      to choose among the various existing return points; it must be
  264.      `eq' to the value specified in the `catch'.  If multiple return
  265.      points match TAG, the innermost one is used.
  266.  
  267.      The argument VALUE is used as the value to return from that
  268.      `catch'.
  269.  
  270.      If no return point is in effect with tag TAG, then a `no-catch'
  271.      error is signaled with data `(TAG VALUE)'.
  272.  
  273.  
  274. 
  275. File: elisp,  Node: Examples of Catch,  Next: Errors,  Prev: Catch and Throw,  Up: Nonlocal Exits
  276.  
  277. Examples of `catch' and `throw'
  278. -------------------------------
  279.  
  280.    One way to use `catch' and `throw' is to exit from a doubly nested
  281. loop.  (In most languages, this would be done with a "go to".) Here
  282. we compute `(foo I J)' for I and J varying from 0 to 9:
  283.  
  284.      (defun search-foo ()
  285.        (catch 'loop
  286.          (let ((i 0))
  287.            (while (< i 10)
  288.              (let ((j 0))
  289.                (while (< j 10)
  290.                  (if (foo i j)
  291.                      (throw 'loop (list i j)))
  292.                  (setq j (1+ j))))
  293.              (setq i (1+ i))))))
  294.  
  295. If `foo' ever returns non-`nil', we stop immediately and return a
  296. list of I and J.  If `foo' always returns `nil', the `catch' returns
  297. normally, and the value is `nil', since that is the result of the
  298. `while'.
  299.  
  300.    Here are two tricky examples, slightly different, showing two
  301. return points at once.  First, two return points with the same tag,
  302. `hack':
  303.  
  304.      (defun catch2 (tag)
  305.        (catch tag
  306.          (throw 'hack 'yes)))
  307.      => catch2
  308.      
  309.      (catch 'hack 
  310.        (print (catch2 'hack))
  311.        'no)
  312.      -| yes
  313.      => no
  314.  
  315. Since both return points have tags that match the `throw', it goes to
  316. the inner one, the one established in `catch2'.  Therefore, `catch2'
  317. returns normally with value `yes', and this value is printed. 
  318. Finally the second body form in the outer `catch', which is `'no', is
  319. evaluated and returned from the outer `catch'.
  320.  
  321.    Now let's change the argument given to `catch2':
  322.  
  323.      (defun catch2 (tag)
  324.        (catch tag
  325.          (throw 'hack 'yes)))
  326.      => catch2
  327.      
  328.      (catch 'hack
  329.        (print (catch2 'quux))
  330.        'no)
  331.      => yes
  332.  
  333. We still have two return points, but this time only the outer one has
  334. the tag `hack'; the inner one has the tag `quux' instead.  Therefore,
  335. the `throw' returns the value `yes' from the outer return point.  The
  336. function `print' is never called, and the body-form `'no' is never
  337. evaluated.
  338.  
  339.  
  340. 
  341. File: elisp,  Node: Errors,  Next: Cleanups,  Prev: Examples of Catch,  Up: Nonlocal Exits
  342.  
  343. Errors
  344. ------
  345.  
  346.    When Emacs Lisp attempts to evaluate a form that, for some reason,
  347. cannot be evaluated, it "signals" an "error".
  348.  
  349.    When an error is signaled, Emacs's default reaction is to print an
  350. error message and terminate execution of the current command.  This
  351. is the right thing to do in most cases, such as if you type `C-f' at
  352. the end of the buffer.
  353.  
  354.    In complicated programs, simple termination may not be what you
  355. want.  For example, the program may have made temporary changes in
  356. data structures, or created temporary buffers which should be deleted
  357. before the program is finished.  In such cases, you would use
  358. `unwind-protect' to establish "cleanup expressions" to be evaluated
  359. in case of error.  Occasionally, you may wish the program to continue
  360. execution despite an error in a subroutine.  In these cases, you
  361. would use `condition-case' to establish "error handlers" to recover
  362. control in case of error.
  363.  
  364.    Resist the temptation to use error handling to transfer control
  365. from one part of the program to another; use `catch' and `throw'. 
  366. *Note Catch and Throw::.
  367.  
  368. * Menu:
  369.  
  370. * Signaling Errors::      How to report an error.
  371. * Processing of Errors::  What Emacs does when you report an error.
  372. * Handling Errors::       How you can trap errors and continue execution.
  373. * Error Names::           How errors are classified for trapping them.
  374.  
  375.  
  376. 
  377. File: elisp,  Node: Signaling Errors,  Next: Processing of Errors,  Prev: Errors,  Up: Errors
  378.  
  379. How to Signal an Error
  380. ......................
  381.  
  382.     Most errors are signaled "automatically" within Lisp primitives
  383. which you call for other purposes, such as if you try to take the CAR
  384. of an integer or move forward a character at the end of the buffer;
  385. you can also signal errors explicitly with the functions `error' and
  386. `signal'.
  387.  
  388.  * Function: error FORMAT-STRING &rest ARGS
  389.      This function signals an error with an error message constructed
  390.      by applying `format' (*note String Conversion::.) to
  391.      FORMAT-STRING and ARGS.
  392.  
  393.      Typical uses of `error' is shown in the following examples:
  394.  
  395.           (error "You have committed an error.  Try something else.")
  396.                error--> You have committed an error.  Try something else.
  397.           
  398.           (error "You have committed %d errors.  You don't learn fast." 10)
  399.                error--> You have committed 10 errors.  You don't learn fast.
  400.  
  401.       `error' works by calling `signal' with two arguments: the error
  402.      symbol `error', and a list containing the string returned by
  403.      `format'.
  404.  
  405.      If you want to use a user-supplied string as an error message
  406.      verbatim, don't just write `(error STRING)'.  If STRING contains
  407.      `%', it will be interpreted as a format specifier, with
  408.      undesirable results.  Instead, use `(error "%s" STRING)'.
  409.  
  410.  * Function: signal ERROR-SYMBOL DATA
  411.      This function signals an error named by ERROR-SYMBOL.  The
  412.      argument DATA is a list of additional Lisp objects relevant to
  413.      the circumstances of the error.
  414.  
  415.      The argument ERROR-SYMBOL must be an "error symbol"--a symbol
  416.      that has an `error-conditions' property whose value is a list of
  417.      condition names.  This is how different sorts of errors are
  418.      classified.
  419.  
  420.      The number and significance of the objects in DATA depends on
  421.      ERROR-SYMBOL.  For example, with a `wrong-type-arg' error, there
  422.      are two objects in the list: a predicate which describes the
  423.      type that was expected, and the object which failed to fit that
  424.      type.  *Note Error Names::, for a description of error symbols.
  425.  
  426.      Both ERROR-SYMBOL and DATA are available to any error handlers
  427.      which handle the error: a list `(ERROR-SYMBOL . DATA)' is
  428.      constructed to become the value of the local variable bound in
  429.      the `condition-case' form (*note Handling Errors::.).  If the
  430.      error is not handled, both of them are used in printing the
  431.      error message.
  432.  
  433.           (signal 'wrong-number-of-arguments '(x y))
  434.                error--> Wrong number of arguments: x, y
  435.           
  436.           (signal 'no-such-error '("My unknown error condition."))
  437.                error--> peculiar error: "My unknown error condition."
  438.  
  439.      Common Lisp note: Emacs Lisp has nothing like the Common Lisp
  440.      concept of continuable errors.
  441.  
  442.  
  443. 
  444. File: elisp,  Node: Processing of Errors,  Next: Handling Errors,  Prev: Signaling Errors,  Up: Errors
  445.  
  446. How Emacs Processes Errors
  447. ..........................
  448.  
  449.     When an error is signaled, Emacs searches for an active "handler"
  450. for the error.  A handler is a specially marked place in the Lisp
  451. code of the current function or any of the functions by which it was
  452. called.  If an applicable handler exists, its code is executed, and
  453. control resumes following the handler.  The handler executes in the
  454. environment of the `condition-case' which established it; all
  455. functions called within that `condition-case' have already been
  456. exited, and the handler cannot return to them.
  457.  
  458.    If no applicable handler is in effect in your program, the current
  459. command is terminated and control returns to the editor command loop,
  460. because the command loop has an implicit handler for all kinds of
  461. errors.  The command loop's handler uses the error symbol and
  462. associated data to print an error message.
  463.  
  464.    When an error is not handled explicitly, it may cause the Lisp
  465. debugger to be called.  The debugger is enabled if the variable
  466. `debug-on-error' (*note Error Debugging::.) is non-`nil'.  Unlike
  467. error handlers, the debugger runs in the environment of the error, so
  468. that you can examine values of variables precisely as they were at
  469. the time of the error.
  470.  
  471.  
  472. 
  473. File: elisp,  Node: Handling Errors,  Next: Error Names,  Prev: Processing of Errors,  Up: Errors
  474.  
  475. Writing Code to Handle Errors
  476. .............................
  477.  
  478.     The usual effect of signaling an error is to terminate the command
  479. that is running and return immediately to the Emacs editor command
  480. loop.  You can arrange to trap errors occurring in a part of your
  481. program by establishing an "error handler" with the special form
  482. `condition-case'.  A simple example looks like this:
  483.  
  484.      (condition-case nil
  485.          (delete-file filename)
  486.        (error nil))
  487.  
  488. This deletes the file named FILENAME, catching any error and
  489. returning `nil' if an error occurs.
  490.  
  491.    The second argument of `condition-case' is called the "protected
  492. form".  (In the example above, the protected form is a call to
  493. `delete-file'.)  The error handlers go into effect when this form
  494. begins execution and are deactivated when this form returns.  They
  495. remain in effect for all the intervening time.  In particular, they
  496. are in effect during the execution of subroutines called by this
  497. form, and their subroutines, and so on.  This is a good thing, since,
  498. strictly speaking, errors can be signaled only by Lisp primitives
  499. (including `signal' and `error') called by the protected form, not by
  500. the protected form itself.
  501.  
  502.    The arguments after the protected form are handlers.  Each handler
  503. lists one or more "condition names" (which are symbols) to specify
  504. which errors it will handle.  The error symbol specified when an
  505. error is signaled also defines a list of condition names.  A handler
  506. applies to an error if they have any condition names in common.  In
  507. the example above, there is one handler, and it specifies one
  508. condition name, `error', which covers all errors.
  509.  
  510.    The search for an applicable handler checks all the established
  511. handlers starting with the most recently established one.  Thus, if
  512. two nested `condition-case' forms try to handle the same error, the
  513. inner of the two will actually handle it.
  514.  
  515.    When an error is handled, control returns to the handler,
  516. unbinding all variable bindings made by binding constructs that are
  517. exited and executing the cleanups of all `unwind-protect' forms that
  518. are exited by doing so.  Then the body of the handler is executed. 
  519. After this, execution continues by returning from the
  520. `condition-case' form.  Because the protected form is exited
  521. completely before execution of the handler, the handler cannot resume
  522. execution at the point of the error, nor can it examine variable
  523. bindings that were made within the protected form.  All it can do is
  524. clean up and proceed.
  525.  
  526.    Error signaling and handling have some resemblance to `throw' and
  527. `catch', but they are entirely separate facilities.  An error cannot
  528. be caught by a `catch', and a `throw' cannot be handled by an error
  529. handler (though if there is no `catch', `throw' will signal an error
  530. which can be handled).
  531.  
  532.  * Special Form: condition-case VAR PROTECTED-FORM HANDLERS...
  533.      This special form establishes the error handlers HANDLERS around
  534.      the execution of PROTECTED-FORM.  If PROTECTED-FORM executes
  535.      without error, the value it returns becomes the value of the
  536.      `condition-case' form; in this case, the `condition-case' has no
  537.      effect.  The `condition-case' form makes a difference when an
  538.      error occurs during PROTECTED-FORM.
  539.  
  540.      Each of the HANDLERS is a list of the form `(CONDITIONS
  541.      BODY...)'.  CONDITIONS is a condition name to be handled, or a
  542.      list of condition names; BODY is one or more Lisp expressions to
  543.      be executed when this handler handles an error.
  544.  
  545.      Each error that occurs has an "error symbol" which describes
  546.      what kind of error it is.  The `error-conditions' property of
  547.      this symbol is a list of condition names (*note Error Names::.).
  548.      Emacs searches all the active `condition-case' forms for a
  549.      handler which specifies one or more of these names; the
  550.      innermost matching `condition-case' handles the error.  The
  551.      handlers in this `condition-case' are tested in the order in
  552.      which they appear.
  553.  
  554.      The body of the handler is then executed, and the
  555.      `condition-case' returns normally, using the value of the last
  556.      form in the body as the overall value.
  557.  
  558.      The argument VAR is a variable.  `condition-case' does not bind
  559.      this variable when executing the PROTECTED-FORM, only when it
  560.      handles an error.  At that time, VAR is bound locally to a list
  561.      of the form `(ERROR-SYMBOL . DATA)', giving the particulars of
  562.      the error.  The handler can refer to this list to decide what to
  563.      do.  For example, if the error is for failure opening a file,
  564.      the file name is the second element of DATA--the third element
  565.      of VAR.
  566.  
  567.      If VAR is `nil', that means no variable is bound.  Then the
  568.      error symbol and associated data are not made available to the
  569.      handler.
  570.  
  571.    Here is an example of using `condition-case' to handle the error
  572. that results from dividing by zero.  The handler prints out a warning
  573. message and returns a very large number.
  574.  
  575.      (defun safe-divide (dividend divisor)
  576.        (condition-case err                
  577.            ;; Protected form.
  578.            (/ dividend divisor)              
  579.          ;; The handler.
  580.          (arith-error                        ; Condition.
  581.           (princ (format "Arithmetic error: %s" err))
  582.           1000000)))
  583.      => safe-divide
  584.      
  585.      (safe-divide 5 0)
  586.           -| Arithmetic error: (arith-error)
  587.      => 1000000
  588.  
  589. The handler specifies condition name `arith-error' so that it will
  590. handle only division-by-zero errors.  Other kinds of errors will not
  591. be handled, at least not by this `condition-case'.  Thus,
  592.  
  593.      (safe-divide nil 3)
  594.           error--> Wrong type argument: integer-or-marker-p, nil
  595.  
  596.    Here is a `condition-case' that catches all kinds of errors,
  597. including those signaled with `error':
  598.  
  599.      (setq baz 34)
  600.           => 34
  601.      
  602.      (condition-case err
  603.          (if (eq baz 35)
  604.              t
  605.            ;; This is a call to the function `error'.
  606.            (error "Rats!  The variable %s was %s, not 35." 'baz baz))
  607.        ;; This is the handler; it is not a form.
  608.        (error (princ (format "The error was: %s" err)) 
  609.               2))
  610.      
  611.           -| The error was: (error "Rats!  The variable baz was 34, not 35.")
  612.           => 2
  613.  
  614.    `condition-case' is often used to trap errors that are
  615. predictable, such as failure to open a file in a call to
  616. `insert-file-contents'.  It is also used to trap errors that are
  617. totally unpredictable, such as when the program evaluates an
  618. expression read from the user.
  619.  
  620.  
  621. 
  622. File: elisp,  Node: Error Names,  Prev: Handling Errors,  Up: Errors
  623.  
  624. Error Symbols and Condition Names
  625. .................................
  626.  
  627.     When you signal an error, you specify an "error symbol" to specify
  628. the kind of error you have in mind.  Each error has one and only one
  629. error symbol to categorize it.  This is the finest classification of
  630. errors defined by the Lisp language.
  631.  
  632.    These narrow classifications are grouped into a hierarchy of wider
  633. classes called "error conditions", identified by "condition names". 
  634. The narrowest such classes belong to the error symbols themselves:
  635. each error symbol is also a condition name.  There are also condition
  636. names for more extensive classes, up to the condition name `error'
  637. which takes in all kinds of errors.  Thus, each error has one or more
  638. condition names: `error', the error symbol if that is distinct from
  639. `error', and perhaps some intermediate classifications.
  640.  
  641.    In order for a symbol to be usable as an error symbol, it must
  642. have an `error-conditions' property which gives a list of condition
  643. names.  This list defines the conditions which this kind of error
  644. belongs to.  (The error symbol itself, and the symbol `error', should
  645. always be members of this list.)  Thus, the hierarchy of condition
  646. names is defined by the `error-conditions' properties of the error
  647. symbols.
  648.  
  649.    In addition to the `error-conditions' list, the error symbol
  650. should have an `error-message' property whose value is a string to be
  651. printed when that error is signaled but not handled.  If the
  652. `error-message' property exists, but is not a string, the error
  653. message `peculiar error' is used.
  654.  
  655.    Here is how we define a new error symbol, `new-error':
  656.  
  657.      (put 'new-error 'error-conditions '(error my-own-errors new-error))
  658.           => (error my-own-errors new-error)
  659.      (put 'new-error 'error-message "A new error")
  660.           => "A new error"
  661.  
  662. This error has three condition names: `new-error', the narrowest
  663. classification; `my-own-errors', which we imagine is a wider
  664. classification; and `error', which is the widest of all.
  665.  
  666.    Naturally, Emacs will never signal a `new-error' on its own; only
  667. an explicit call to `signal' (*note Errors::.) in your code can do
  668. this:
  669.  
  670.      (signal 'new-error '(x y))
  671.           error--> A new error: x, y
  672.  
  673.    This error can be handled through any of the three condition names.
  674. This example handles `new-error' and any other errors in the class
  675. `my-own-errors':
  676.  
  677.      (condition-case foo
  678.          (bar nil t)
  679.        (my-own-errors nil))
  680.  
  681.    The significant way that errors are classified is by their
  682. condition names--the names used to match errors with handlers.  An
  683. error symbol serves only as a convenient way to specify the intended
  684. error message and list of condition names.  If `signal' were given a
  685. list of condition names rather than one error symbol, that would be
  686. cumbersome.
  687.  
  688.    By contrast, using only error symbols without condition names
  689. would seriously decrease the power of `condition-case'.  Condition
  690. names make it possible to categorize errors at various levels of
  691. generality when you write an error handler.  Using error symbols
  692. alone would eliminate all but the narrowest level of classification.
  693.  
  694.    *Note Standard Errors::, for a list of all the standard error
  695. symbols and their conditions.
  696.  
  697.  
  698. 
  699. File: elisp,  Node: Cleanups,  Prev: Errors,  Up: Nonlocal Exits
  700.  
  701. Cleaning up from Nonlocal Exits
  702. -------------------------------
  703.  
  704.    The `unwind-protect' construct is essential whenever you
  705. temporarily put a data structure in an inconsistent state; it permits
  706. you to ensure the data are consistent in the event of an error.
  707.  
  708.  * Special Form: unwind-protect BODY CLEANUP-FORMS...
  709.      `unwind-protect' executes the BODY with a guarantee that the
  710.      CLEANUP-FORMS will be evaluated if control leaves BODY, no
  711.      matter how that happens.  The BODY may complete normally, or
  712.      execute a `throw' out of the `unwind-protect', or cause an
  713.      error; in all cases, the CLEANUP-FORMS will be evaluated.
  714.  
  715.      Only the BODY is actually protected by the `unwind-protect'.  If
  716.      any of the CLEANUP-FORMS themselves exit nonlocally (e.g., via a
  717.      `throw' or an error), it is *not* guaranteed that the rest of
  718.      them will be executed.  If the failure of one of the
  719.      CLEANUP-FORMS has the potential to cause trouble, then it should
  720.      be protected by another `unwind-protect' around that form.
  721.  
  722.      The number of currently active `unwind-protect' forms counts,
  723.      together with the number of local variable bindings, against the
  724.      limit `max-specpdl-size' (*note Local Variables::.).
  725.  
  726.    For example, here we make an invisible buffer for temporary use,
  727. and make sure to kill it before finishing:
  728.  
  729.      (save-excursion
  730.        (let ((buffer (get-buffer-create " *temp*")))
  731.          (set-buffer buffer)
  732.          (unwind-protect
  733.              BODY
  734.            (kill-buffer buffer))))
  735.  
  736. You might think that we could just as well write `(kill-buffer
  737. (current-buffer))' and dispense with the variable `buffer'.  However,
  738. the way shown above is safer, if BODY happens to get an error after
  739. switching to a different buffer!  (Alternatively, you could write
  740. another `save-excursion' around the body, to ensure that the
  741. temporary buffer becomes current in time to kill it.)
  742.  
  743.    Here is an actual example taken from the file `ftp.el'.  It
  744. creates a process (*note Processes::.) to try to establish a
  745. connection to a remote machine.  As the function `ftp-login' is
  746. highly susceptible to numerous problems which the writer of the
  747. function cannot anticipate, it is protected with a form that
  748. guarantees deletion of the process in the event of failure. 
  749. Otherwise, Emacs might fill up with useless subprocesses.
  750.  
  751.      (let ((win nil))
  752.        (unwind-protect
  753.            (progn
  754.              (setq process (ftp-setup-buffer host file))
  755.              (if (setq win (ftp-login process host user password))
  756.                  (message "Logged in")
  757.                (error "Ftp login failed")))
  758.          (or win (and process (delete-process process)))))
  759.  
  760.    This example actually has a small bug: if the user types `C-g' to
  761. quit, and the quit happens immediately after the function
  762. `ftp-setup-buffer' returns but before the variable `process' is set,
  763. the process will not be killed.  There is no easy way to fix this
  764. bug, but at least it is very unlikely.
  765.  
  766.  
  767. 
  768. File: elisp,  Node: Variables,  Next: Functions,  Prev: Control Structures,  Up: Top
  769.  
  770. Variables
  771. *********
  772.  
  773.    A "variable" is a name used in a program to stand for a value. 
  774. Nearly all programming languages have variables of some sort.  In the
  775. text for a Lisp program, variables are written using the syntax for
  776. symbols.
  777.  
  778.    In Lisp, unlike most programming languages, programs are
  779. represented primarily as Lisp objects and only secondarily as text. 
  780. The Lisp objects used for variables are symbols: the symbol name is
  781. the variable name, and the variable's value is stored in the value
  782. cell of the symbol.  The use of a symbol as a variable is independent
  783. of whether the same symbol has a function definition.  *Note Symbol
  784. Components::.
  785.  
  786.    The textual form of a program is determined by its Lisp object
  787. representation; it is the read syntax for the Lisp object which
  788. constitutes the program.  This is why a variable in a textual Lisp
  789. program is written as the read syntax for the symbol that represents
  790. the variable.
  791.  
  792. * Menu:
  793.  
  794. * Global Variables::      Variable values that exist permanently, everywhere.
  795. * Constant Variables::    Certain "variables" have values that never change.
  796. * Local Variables::       Variable values that exist only temporarily.
  797. * Void Variables::        Symbols that lack values.
  798. * Defining Variables::    A definition says a symbol is used as a variable.
  799. * Accessing Variables::   Examining values of variables whose names
  800.                             are known only at run time.
  801. * Setting Variables::     Storing new values in variables.
  802. * Variable Scoping::      How Lisp chooses among local and global values.
  803. * Buffer-Local Variables::  Variable values in effect only in one buffer.
  804.  
  805.  
  806. 
  807. File: elisp,  Node: Global Variables,  Next: Constant Variables,  Prev: Variables,  Up: Variables
  808.  
  809. Global Variables
  810. ================
  811.  
  812.    The simplest way to use a variable is "globally".  This means that
  813. the variable has just one value at a time, and this value is in
  814. effect (at least for the moment) throughout the Lisp system.  The
  815. value remains in effect until you specify a new one.  When a new
  816. value replaces the old one, no trace of the old value remains in the
  817. variable.
  818.  
  819.    You specify a value for a symbol with `setq'.  For example,
  820.  
  821.      (setq x '(a b))
  822.  
  823. gives the variable `x' the value `(a b)'.  Note that the first
  824. argument of `setq', the name of the variable, is not evaluated, but
  825. the second argument, the desired value, is evaluated normally.
  826.  
  827.    Once the variable has a value, you can refer to it by using the
  828. symbol by itself as an expression.  Thus,
  829.  
  830.      x
  831.           => (a b)
  832.  
  833. assuming the `setq' form shown above has already been executed.
  834.  
  835.    If you do another `setq', the new value replaces the old one:
  836.  
  837.      x
  838.           => (a b)
  839.      (setq x 4)
  840.           => 4
  841.      x
  842.           => 4
  843.  
  844.  
  845. 
  846. File: elisp,  Node: Constant Variables,  Next: Local Variables,  Prev: Global Variables,  Up: Variables
  847.  
  848. Variables that Never Change
  849. ===========================
  850.  
  851.    Emacs Lisp has two special symbols, `nil' and `t', that always
  852. evaluate to themselves.  These symbols cannot be rebound, nor can
  853. their value cells be changed.  An attempt to change the value of
  854. `nil' or `t' signals a `setting-constant' error.
  855.  
  856.      nil == 'nil
  857.           => nil
  858.      (setq nil 500)
  859.      error--> Attempt to set constant symbol: nil
  860.  
  861.  
  862. 
  863. File: elisp,  Node: Local Variables,  Next: Void Variables,  Prev: Constant Variables,  Up: Variables
  864.  
  865. Local Variables
  866. ===============
  867.  
  868.    Global variables are given values that last until explicitly
  869. superseded with new values.  Sometimes it is useful to create
  870. variable values that exist temporarily--only while within a certain
  871. part of the program.  These values are called "local", and the
  872. variables so used are called "local variables".
  873.  
  874.    For example, when a function is called, its argument variables
  875. receive new local values which last until the function exits. 
  876. Similarly, the `let' special form explicitly establishes new local
  877. values for specified variables; these last until exit from the `let'
  878. form.
  879.  
  880.    When a local value is established, the previous value (or lack of
  881. one) of the variable is saved away.  When the life span of the local
  882. value is over, the previous value is restored.  In the mean time, we
  883. say that the previous value is "shadowed" and "not visible".  Both
  884. global and local values may be shadowed.
  885.  
  886.    If you set a variable (such as with `setq') while it is local,
  887. this replaces the local value; it does not alter the global value, or
  888. previous local values that are shadowed.  To model this behavior, we
  889. speak of a "local binding" of the variable as well as a local value.
  890.  
  891.    The local binding is a conceptual place that holds a local value. 
  892. Entry to a function, or a special form such as `let', creates the
  893. local binding; exit from the function or from the `let' removes the
  894. local binding.  As long as the local binding lasts, the variable's
  895. value is stored within it.  Use of `setq' or `set' while there is a
  896. local binding stores a different value into the local binding; it
  897. does not create a new binding.
  898.  
  899.    We also speak of the "global binding", which is where
  900. (conceptually) the global value is kept.
  901.  
  902.    A variable can have more than one local binding at a time (for
  903. example, if there are nested `let' forms that bind it).  In such a
  904. case, the most recently created local binding that still exists is
  905. the "current binding" of the variable.  (This is called "dynamic
  906. scoping"; see *Note Variable Scoping::.)  If there are no local
  907. bindings, the variable's global binding is its current binding.  We
  908. also call the current binding the "most-local existing binding", for
  909. emphasis.  Ordinary evaluation of a symbol always returns the value
  910. of its current binding.
  911.  
  912.    The special forms `let' and `let*' exist to create local bindings.
  913.  
  914.  * Special Form: let (BINDINGS...) FORMS...
  915.      This function binds variables according to BINDINGS and then
  916.      evaluates all of the FORMS in textual order.  The `let'-form
  917.      returns the value of the last form in FORMS.
  918.  
  919.      Each of the BINDINGS is either (i) a symbol, in which case that
  920.      symbol is bound to `nil'; or (ii) a list of the form `(SYMBOL
  921.      VALUE-FORM)', in which case SYMBOL is bound to the result of
  922.      evaluating VALUE-FORM.  If VALUE-FORM is omitted, `nil' is used.
  923.  
  924.      All of the VALUE-FORMs in BINDINGS are evaluated in the order
  925.      they appear and *before* any of the symbols are bound.  Here is
  926.      an example of this: `Z' is bound to the old value of `Y', which
  927.      is 2, not the new value, 1.
  928.  
  929.           (setq Y 2)
  930.                => 2
  931.           (let ((Y 1) 
  932.                 (Z Y))
  933.             (list Y Z))
  934.                => (1 2)
  935.  
  936.  * Special Form: let* (BINDINGS...) FORMS...
  937.      This special form is like `let', except that each symbol in
  938.      BINDINGS is bound as soon as its new value is computed, before
  939.      the computation of the values of the following local bindings. 
  940.      Therefore, an expression in BINDINGS may reasonably refer to the
  941.      preceding symbols bound in this `let*' form.  Compare the
  942.      following example with the example above for `let'.
  943.  
  944.           (setq Y 2)
  945.                => 2
  946.           (let* ((Y 1)
  947.                  (Z Y))    ; Use the just-established value of `Y'.
  948.             (list Y Z))
  949.                => (1 1)
  950.  
  951.    Here is a complete list of the other facilities which create local
  952. bindings:
  953.  
  954.    * Function calls (*note Functions::.).
  955.  
  956.    * Macro calls (*note Macros::.).
  957.  
  958.    * `condition-case' (*note Errors::.).
  959.  
  960.  * Variable: max-specpdl-size
  961.      This variable defines the limit on the number of local variable
  962.      bindings and `unwind-protect' cleanups (*note Nonlocal Exits::.)
  963.      that are allowed before signaling an error (with data `"Variable
  964.      binding depth exceeds max-specpdl-size"').
  965.  
  966.      This limit, with the associated error when it is exceeded, is
  967.      one way that Lisp avoids infinite recursion on an ill-defined
  968.      function.
  969.  
  970.      The default value is 600.
  971.  
  972.  
  973. 
  974. File: elisp,  Node: Void Variables,  Next: Defining Variables,  Prev: Local Variables,  Up: Variables
  975.  
  976. When a Variable is "Void"
  977. =========================
  978.  
  979.    If you have never given a symbol any value as a global variable,
  980. we say that that symbol's global value is "void".  In other words,
  981. the symbol's value cell does not have any Lisp object in it.  If you
  982. try to evaluate the symbol, you get a `void-variable' error rather
  983. than a value.
  984.  
  985.    Note that a value of `nil' is not the same as void.  The symbol
  986. `nil' is a Lisp object and can be the value of a variable just as any
  987. other object can be; but it is *a value*.  A void variable does not
  988. have any value.
  989.  
  990.    After you have given a variable a value, you can make it void once
  991. more using `makunbound'.
  992.  
  993.  * Function: makunbound SYMBOL
  994.      This function makes the current binding of SYMBOL void.  This
  995.      causes any future attempt to use this symbol as a variable to
  996.      signal the error `void-variable', unless or until you set it
  997.      again.
  998.  
  999.      `makunbound' returns SYMBOL.
  1000.  
  1001.           (makunbound 'x)          ; Make the global value of `x' void.
  1002.                => x
  1003.           x
  1004.           error--> Symbol's value as variable is void: x
  1005.  
  1006.      If SYMBOL is locally bound, `makunbound' affects the most local
  1007.      existing binding.  This is the only way a symbol can have a void
  1008.      local binding, since all the constructs that create local
  1009.      bindings create them with values.  In this case, the voidness
  1010.      lasts at most as long as the binding does; when the binding is
  1011.      removed due to exit from the construct that made it, the
  1012.      previous or global binding is reexposed as usual, and the
  1013.      variable is no longer void unless the newly reexposed binding
  1014.      was void all along.
  1015.  
  1016.           (setq x 1)               ; Put a value in the global binding.
  1017.                => 1
  1018.           (let ((x 2))             ; Locally bind it.
  1019.             (makunbound 'x)        ; Void the local binding.
  1020.             x)
  1021.           error--> Symbol's value as variable is void: x
  1022.           x                        ; The global binding is unchanged.
  1023.                => 1
  1024.           
  1025.           (let ((x 2))             ; Locally bind it.
  1026.             (let ((x 3))           ; And again.
  1027.               (makunbound 'x)      ; Void the innermost-local binding.
  1028.               x))                  ; And refer: it's void.
  1029.           error--> Symbol's value as variable is void: x
  1030.           
  1031.           (let ((x 2))
  1032.             (let ((x 3))
  1033.               (makunbound 'x))     ; Void inner binding, then remove it.
  1034.             x)                     ; Now outer `let' binding is visible.
  1035.                => 2
  1036.  
  1037.    A variable that has been made void with `makunbound' is
  1038. indistinguishable from one that has never received a value and has
  1039. always been void.
  1040.  
  1041.    You can use the function `boundp' to test whether a variable is
  1042. currently void.
  1043.  
  1044.  * Function: boundp VARIABLE
  1045.      `boundp' returns `t' if VARIABLE (a symbol) is not void; more
  1046.      precisely, if its current binding is not void.  It returns `nil'
  1047.      otherwise.
  1048.  
  1049.           (boundp 'abracadabra)                ; Starts out void.
  1050.                => nil
  1051.           (let ((abracadabra 5))               ; Locally bind it.
  1052.             (boundp 'abracadabra))
  1053.                => t
  1054.           (boundp 'abracadabra)                ; Still globally void.
  1055.                => nil
  1056.           (setq abracadabra 5)                 ; Make it globally nonvoid.
  1057.                => 5
  1058.           (boundp 'abracadabra)
  1059.                => t
  1060.  
  1061.  
  1062. 
  1063. File: elisp,  Node: Defining Variables,  Next: Accessing Variables,  Prev: Void Variables,  Up: Variables
  1064.  
  1065. Defining Global Variables
  1066. =========================
  1067.  
  1068.    You may announce your intention to use a symbol as a global
  1069. variable with a definition, using `defconst' or `defvar'.
  1070.  
  1071.    In Emacs Lisp, definitions serve three purposes.  First, they
  1072. inform the user who reads the code that certain symbols are
  1073. *intended* to be used as variables.  Second, they inform the Lisp
  1074. system of these things, supplying a value and documentation.  Third,
  1075. they provide information to utilities such as `etags' and
  1076. `make-docfile', which create data bases of the functions and
  1077. variables in a program.
  1078.  
  1079.    The difference between `defconst' and `defvar' is primarily a
  1080. matter of intent, serving to inform human readers of whether programs
  1081. will change the variable.  Emacs Lisp does not restrict the ways in
  1082. which a variable can be used based on `defconst' or `defvar'
  1083. declarations.  However, it also makes a difference for
  1084. initialization: `defconst' unconditionally initializes the variable,
  1085. while `defvar' initializes it only if it is void.
  1086.  
  1087.    One would expect user option variables to be defined with
  1088. `defconst', since programs do not change them.  Unfortunately, this
  1089. has bad results if the definition is in a library that is not
  1090. preloaded: `defconst' would override any prior value when the library
  1091. is loaded.  Users would like to be able to set the option in their
  1092. init files, and override the default value given in the definition. 
  1093. For this reason, user options must be defined with `defvar'.
  1094.  
  1095.  * Special Form: defvar SYMBOL [VALUE [DOC-STRING]]
  1096.      This special form informs a person reading your code that SYMBOL
  1097.      will be used as a variable that the programs are likely to set
  1098.      or change.  It is also used for all user option variables except
  1099.      in the preloaded parts of Emacs.  Note that SYMBOL is not
  1100.      evaluated; the symbol to be defined must appear explicitly in
  1101.      the `defvar'.
  1102.  
  1103.      If SYMBOL already has a value (i.e., it is not void), VALUE is
  1104.      not even evaluated, and SYMBOL's value remains unchanged.  If
  1105.      SYMBOL is void and VALUE is specified, it is evaluated and
  1106.      SYMBOL is set to the result.  (If VALUE is not specified, the
  1107.      value of SYMBOL is not changed in any case.)
  1108.  
  1109.      If the DOC-STRING argument appears, it specifies the
  1110.      documentation for the variable.  (This opportunity to specify
  1111.      documentation is one of the main benefits of defining the
  1112.      variable.)  The documentation is stored in the symbol's
  1113.      `variable-documentation' property.  The Emacs help functions
  1114.      (*note Documentation::.) look for this property.
  1115.  
  1116.      If the first character of DOC-STRING is `*', it means that this
  1117.      variable is considered to be a user option.  This affects
  1118.      commands such as `set-variable' and `edit-options'.
  1119.  
  1120.      For example, this form defines `foo' but does not set its value:
  1121.  
  1122.           (defvar foo)
  1123.                => foo
  1124.  
  1125.      The following example sets the value of `bar' to `23', and gives
  1126.      it a documentation string:
  1127.  
  1128.           (defvar bar 23 "The normal weight of a bar.")
  1129.                => bar
  1130.  
  1131.      The following form changes the documentation string for `bar',
  1132.      making it a user option, but does not change the value, since
  1133.      `bar' already has a value.  (The addition `(1+ 23)' is not even
  1134.      performed.)
  1135.  
  1136.           (defvar bar (1+ 23) "*The normal weight of a bar.")
  1137.                => bar
  1138.           bar
  1139.                => 23
  1140.  
  1141.      Here is an equivalent expression for the `defvar' special form:
  1142.  
  1143.           (defvar SYMBOL VALUE DOC-STRING)
  1144.           ==
  1145.           (progn
  1146.             (if (not (boundp 'SYMBOL))
  1147.                 (setq SYMBOL VALUE))
  1148.             (put 'SYMBOL 'variable-documentation 'DOC-STRING)
  1149.             'SYMBOL)
  1150.  
  1151.      The `defvar' form returns SYMBOL, but it is normally used at top
  1152.      level in a file where its value does not matter.
  1153.  
  1154.  * Special Form: defconst SYMBOL [VALUE [DOC-STRING]]
  1155.      This special form informs a person reading your code that SYMBOL
  1156.      has a global value, established here, that will not normally be
  1157.      changed or locally bound by the execution of the program.  The
  1158.      user, however, may be welcome to change it.  Note that SYMBOL is
  1159.      not evaluated; the symbol to be defined must appear explicitly
  1160.      in the `defconst'.
  1161.  
  1162.      `defconst' always evaluates VALUE and sets the global value of
  1163.      SYMBOL to the result, provided VALUE is given.
  1164.  
  1165.      *Note:* don't use `defconst' for user option variables in
  1166.      libraries that are not normally loaded.  The user should be able
  1167.      to specify a value for such a variable in the `.emacs' file, so
  1168.      that it will be in effect if and when the library is loaded
  1169.      later.
  1170.  
  1171.      Here, `pi' is a constant that presumably ought not to be changed
  1172.      by anyone (attempts by the Indiana State Legislature
  1173.      notwithstanding).  As the second form illustrates, however, this
  1174.      is only advisory.
  1175.  
  1176.           (defconst pi 3 "Pi to one place.")
  1177.                => pi
  1178.           (setq pi 4)
  1179.                => pi
  1180.           pi
  1181.                => 4
  1182.  
  1183.  * Function: user-variable-p VARIABLE
  1184.      This function returns `t' if VARIABLE is a user option, intended
  1185.      to be set by the user for customization, `nil' otherwise. 
  1186.      (Variables other than user options exist for the internal
  1187.      purposes of Lisp programs, and users need not know about them.)
  1188.  
  1189.      User option variables are distinguished from other variables by
  1190.      the first character of the `variable-documentation' property. 
  1191.      If the property exists and is a string, and its first character
  1192.      is `*', then the variable is a user option.
  1193.  
  1194.    Note that if the `defconst' and `defvar' special forms are used
  1195. while the variable has a local binding, the local binding's value is
  1196. set, and the global binding is not changed.  This would be confusing.
  1197. But the normal way to use these special forms is at top level in a
  1198. file, where no local binding should be in effect.
  1199.  
  1200.  
  1201. 
  1202. File: elisp,  Node: Accessing Variables,  Next: Setting Variables,  Prev: Defining Variables,  Up: Variables
  1203.  
  1204. Accessing Variable Values
  1205. =========================
  1206.  
  1207.    The usual way to reference a variable is to write the symbol which
  1208. names it (*note Symbol Forms::.).  This requires you to specify the
  1209. variable name when you write the program.  Usually that is exactly
  1210. what you want to do.  Occasionally you need to choose at run time
  1211. which variable to reference; then you can use `symbol-value'.
  1212.  
  1213.  * Function: symbol-value SYMBOL
  1214.      This function returns the value of SYMBOL.  This is the value in
  1215.      the innermost local binding of the symbol, or its global value
  1216.      if it has no local bindings.
  1217.  
  1218.           (setq abracadabra 5)
  1219.                => 5
  1220.           (setq foo 9)
  1221.                => 9
  1222.           
  1223.           ;; Here the symbol `abracadabra'
  1224.           ;; is the symbol whose value is examined.
  1225.           (let ((abracadabra 'foo))
  1226.             (symbol-value 'abracadabra))
  1227.                => foo
  1228.           
  1229.           ;; Here the value of `abracadabra',
  1230.           ;; which is `foo',
  1231.           ;; is the symbol whose value is examined.
  1232.           (let ((abracadabra 'foo))
  1233.             (symbol-value abracadabra))
  1234.                => 9
  1235.           
  1236.           (symbol-value 'abracadabra)
  1237.                => 5
  1238.  
  1239.      A `void-variable' error is signaled if SYMBOL has neither a
  1240.      local binding nor a global value.
  1241.  
  1242.  
  1243.